package com.lepu.app.main.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.app.utils.AppEnterTongJi;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragment;
import com.core.lib.core.AsyncRequest;
import com.eyzhs.app.R;
import com.lepu.app.fun.mother_circle.fragment.FragmentMotherCircle;
import com.lepu.app.fun.mother_circle.fragment.FragmentMotherPost;
import com.lepu.app.fun.my.MySearchActivity;
import com.lepu.app.main.activity.MainTabActivity;

import java.util.ArrayList;

public class MainTabFragmentCircle extends BaseFragment implements AsyncRequest {

	private View mMainView = null;
	private boolean mInitDataSuccess = false;// 是否加载完数据

	private ViewPager mViewPager = null;
	private ImageView mPubContentImageView = null;
	private LinearLayout mTabLayoutOne = null;
	private LinearLayout mTabLayoutTwo = null;
	
	private ArrayList<Fragment> fragmentList;
	private FragmentMotherPost tabOne;
	private FragmentMotherCircle tabTwo;

	private int mCurrentTab = 0;
	
	public boolean mRefreshFlag = true;
	@SuppressLint("InflateParams")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mMainView = LayoutInflater.from(getActivity()).inflate(
				R.layout.main_tab_fragment_circle, null, false);
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		ViewGroup p = (ViewGroup) mMainView.getParent();
		if (p != null) {
			p.removeAllViewsInLayout();
		}
		return mMainView;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInitDataSuccess = false;
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) {
			if (!mInitDataSuccess) {

				 // 第一次进入页面获取数据
	            mRefreshFlag = false;

				mInitDataSuccess = true;
				if(tabOne != null){
					tabOne.requestFocus(false);
				}else{
					init();
				}
				
				mViewPager.setCurrentItem(0);
				updateTabLine(0);
			}
		}
	}

	/*
	 * 初始化ui控件，不要初始化数据！！！！
	 * 
	 * 初始化控件，需要用mMainView.findViewById 不能使用findViewById，否则程序会闪退的
	 */
	private void init() {
		// content
		fragmentList = new ArrayList<Fragment>();
		tabOne = new FragmentMotherPost();
		tabTwo = new FragmentMotherCircle();
		
		fragmentList.add(tabOne);
		fragmentList.add(tabTwo);

		mViewPager = (ViewPager) mMainView.findViewById(R.id.fourViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(getActivity()
				.getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());

		// tab
		mTabLayoutOne = (LinearLayout) mMainView.findViewById(R.id.tabLayout1);
		mTabLayoutTwo = (LinearLayout) mMainView.findViewById(R.id.tabLayout2);
		mPubContentImageView = (ImageView) mMainView
				.findViewById(R.id.pubContentImageView);

		mTabLayoutOne.setOnClickListener(mViewClickListener);
		mTabLayoutTwo.setOnClickListener(mViewClickListener);
		mPubContentImageView.setOnClickListener(mViewClickListener);
	}

	public void setTab(int index)
	{
		mViewPager.setCurrentItem(index,false);
		updateTabLine(index);
	}

	View.OnClickListener mViewClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tabLayout1:
				if (mCurrentTab == 0)
					return;

				setTab(0);
				break;
			case R.id.tabLayout2:

				if (mCurrentTab == 1)
					return;

				setTab(1);
				break;
			case R.id.pubContentImageView:
//				Intent intent2 = new Intent(getActivity(),MotherPostSearchActivity.class);
//				MainTabActivity.getInstance().startActivity(intent2, true);
				
				Intent intentSearch = new Intent(getActivity(),
						MySearchActivity.class);
				intentSearch.putExtra("type", MySearchActivity.TYPE_TOPIC);
				MainTabActivity.getInstance().startActivity(intentSearch, true);
				break;
			default:
				break;
			}
		}
	};

	private void updateTabLine(int currentIndex) {
		if (currentIndex == 0) {
			mTabLayoutOne.setSelected(true);
			mTabLayoutTwo.setSelected(false);
			
			AppEnterTongJi.clickEvent(3, "");
		} else {
			mTabLayoutOne.setSelected(false);
			mTabLayoutTwo.setSelected(true);
			
			AppEnterTongJi.clickEvent(4, "");
		}
	}

	public void refreshBanner(){
		if(fragmentList != null && fragmentList.size() > 0 && tabOne != null){
			tabOne.requestFocus(false);
		}
	}
	
	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mCurrentTab = index;
			mViewPager.setCurrentItem(index,false);
			updateTabLine(index);
		}
	}

	@Override
	public void RequestComplete(Object object, Object data) {
		
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		
	}

}
